package com.student_exam_system.config;


import com.alibaba.fastjson.JSON;
import com.student_exam_system.pojo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * @author fire'ge
 */
@ControllerAdvice
public class ExceptionController {
    @ExceptionHandler(Exception.class)
    @RequestMapping(produces = "application/json;charset=utf-8")
    @ResponseBody
    public String handlerError(Exception ex, HandlerMethod hm, HttpServletResponse response) {
        System.out.println("exception");
        Map<String, Object> map = new HashMap<>();
        map.put("ErrorClass",hm.getBean().getClass().getName());
        map.put("ErrorMethod",hm.getMethod().getName());
        map.put("detailMsg",ex.getMessage());
        map.put("status",false);
       return JSON.toJSONString(Result.fail(500,"请求出现错误",map));
    }
}
